﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoUser;

namespace Zocono.WCS.Domain.UserManageDomain
{
    public class UserService : IUserService
    {
        private readonly IUserRepository _UserRepository;
        private readonly IRoleFunctionRepository _RoleFunctionRepository;
        private readonly IFunctionRepository _FunctionRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;
        private readonly IRoleRepository _Rolepository;

        public UserService(IUserRepository userRepository, IRoleFunctionRepository roleFunctionRepository, IFunctionRepository functionRepository, IMapper mapper, ISnowflake snowflake, IRoleRepository roleRepository)
        {
            _UserRepository = userRepository;
            _RoleFunctionRepository = roleFunctionRepository;
            _FunctionRepository = functionRepository;
            _Mapper = mapper;
            _Snowflake = snowflake;
            _Rolepository = roleRepository;
        }

        public async Task<List<UserInfoDto>> GetAllUserInfoAsync(QueryUserDto where)
        {
            var userInfos = await _UserRepository.GetAllUserInfoAsync(where);
            return userInfos;
            //List<UserInfoDto> usersDto = new List<UserInfoDto>();
            //return _Mapper.Map(userInfos, usersDto);
            //return _Mapper.Map<List<User>>(userInfos);
        }

        public async Task<List<FunctionInfoDto>> GetFunctionInfoAsync()
        {
            List<FunctionInfo> functionInfos = await _FunctionRepository.GetListAsync();
            List<FunctionInfoDto> functionInfoDtos = new List<FunctionInfoDto>();
            return _Mapper.Map(functionInfos, functionInfoDtos);
        }
        public async Task<List<FunctionInfoDto>> GetFunctionInfoAsync(FunctionInfoDto queryFunctionInfoDto)
        {

            var exp = Expressionable.Create<FunctionInfo>();
            exp.AndIF(!string.IsNullOrEmpty(queryFunctionInfoDto.FullName),f => f.FullName.Contains(queryFunctionInfoDto.FullName));
            exp.AndIF(!string.IsNullOrEmpty(queryFunctionInfoDto.URL), f => f.URL.Contains(queryFunctionInfoDto.URL));
            exp.AndIF(queryFunctionInfoDto.Status != 0, f => f.Status == queryFunctionInfoDto.Status);
            List<FunctionInfo> functionInfos = await _FunctionRepository.AsQueryable().Where(exp.ToExpression()).ToListAsync();
            List<FunctionInfoDto> functionInfoDtos = new List<FunctionInfoDto>();
            return _Mapper.Map(functionInfos, functionInfoDtos);
        }

        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> AddFunctionInfoAsync(FunctionInfoDto dto)
        {
            dto.ID = _Snowflake.GetId();
            var info = new FunctionInfo();
            _Mapper.Map(dto, info);
            return await _FunctionRepository.InsertAsync(info);
        }
        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateFunctionInfoAsync(FunctionInfoDto dto)
        {
            var info = new FunctionInfo();
            _Mapper.Map(dto, info);
            return await _FunctionRepository.UpdateAsync(info);
        }
        /// <summary>
        /// 根据编号获取菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FunctionInfoDto> GetFunctionInfoByIdAsync(long id)
        {
            var info = await _FunctionRepository.GetByIdAsync(id);
            var dto = new FunctionInfoDto();
            _Mapper.Map(info, dto);
            return dto;
        }
        /// <summary>
        /// 根据上级菜单ID和状态查询菜单列表
        /// </summary>
        /// <param name="pId">上级菜单ID，0=顶级单</param>
        /// <param name="status">状态1=正常</param>
        /// <returns></returns>
        public async Task<List<FunctionInfoDto>> GetFunctionInfoListByPidAsync(long pId, int status)
        {
            var info = await _FunctionRepository.GetFunctionInfoListByPidAsync(pId, status);
            var dto = new List<FunctionInfoDto>();
            _Mapper.Map(info, dto);
            return dto;
        }
        /// <summary>
        /// 修改菜单状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="status">状态1=启用，2=禁用</param>
        /// <returns></returns>
        public async Task<bool> UpdateStatusByIdAsync(long Id, int status)
        {
            return await _FunctionRepository.UpdateStatusByIdAsync(Id, status);
        }
        public async Task<List<RoleInfoDto>> GetRoleInfoAsync()
        {
            List<RoleInfo> roleInfos = await _Rolepository.GetListAsync();
            List<RoleInfoDto> roleInfoDtos = new List<RoleInfoDto>();
            return _Mapper.Map(roleInfos, roleInfoDtos);

        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> addRoleAsync(RoleInfoDto dto)
        {
            var info = new RoleInfo();
            dto.ID = _Snowflake.GetId();
            _Mapper.Map(dto, info);
            return await _Rolepository.InsertAsync(info);

        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRoleByIdAsync(RoleInfoDto dto)
        {
            var info = new RoleInfo();
            _Mapper.Map(dto, info);
            return await _Rolepository.UpdateAsync(info);
        }
        /// <summary>
        /// 根据编号获取角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RoleInfoDto> GetRoleInfoByIdAsync(long id)
        {
            var info = await _Rolepository.GetByIdAsync(id);
            var dto = new RoleInfoDto();
            _Mapper.Map(info, dto);
            return dto;
        }
        /// <summary>
        /// 根据编号删除角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DelRoleByIdAsync(long id)
        {
            return await _Rolepository.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 获取角色菜单信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public async Task<List<RoleFunctionPage>> GetRoleFunctionByRoleId(long roleID)
        {

            return await _RoleFunctionRepository.GetRoleFunctionByRoleId(roleID);
        }
        /// <summary>
        /// 批量保存角色权限信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRoleFunctionInfoByRoleIdAsync(List<RoleFunctionInfoDto> dto)
        {
            List<RoleFunctionInfo> list = new List<RoleFunctionInfo>();
            foreach (var item in dto)
                item.ID = _Snowflake.GetId();
            _Mapper.Map(dto, list);
            return await _RoleFunctionRepository.UpdateRoleFunctionInfoByRoleIdAsync(list);
        }
    }
}
